#include "outldialog.h"
#include "ui_outldialog.h"
#include<QFile>
#include<QFileDialog>
#include<QMessageBox>
#include<QTextStream>
#include<QDebug>
#include<QTextEdit>
outlDialog::outlDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::outlDialog)
{
    ui->setupUi(this);
    this->setWindowTitle("外来人员信息登记");
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(5);
    ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<"宿舍楼号"<<"姓名"<<"联系电话"<<"登记时间"<<"身份证号");
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableWidget->verticalHeader()->setVisible(false);
    ui->tableWidget->setColumnWidth(0,75);
    ui->tableWidget->setColumnWidth(1,75);
    ui->tableWidget->setColumnWidth(2,100);
    ui->tableWidget->setColumnWidth(3,100);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(4,QHeaderView::Stretch);
    QFont font=ui->tableWidget->horizontalHeader()->font();
    font.setBold(true);
    ui->tableWidget->horizontalHeader()->setFont(font);
}

outlDialog::~outlDialog()
{
    delete ui;
}

void outlDialog::on_pushButton_clicked()
{
    int Rowcount;
    Rowcount=ui->tableWidget->rowCount();
    ui->tableWidget->insertRow(Rowcount);
}

void outlDialog::on_pushButton_2_clicked()
{
    QString filepath="C:\\Users\\Administrator\\Desktop\\QT\\jilu.txt";
    if(!filepath.isEmpty()){
        int row=ui->tableWidget->rowCount();
        int col=ui->tableWidget->columnCount();
        QList<QString> list;
        QString headerRow;
        for(int i=0;i<col;i++){
            headerRow.append(ui->tableWidget->horizontalHeaderItem(i)->text()+",");
        }//写表头
        list.push_back(headerRow);
        for(int i=0;i<row;i++){
            QString rowStr="";
            for(int j=0;j<col;j++){
                if(ui->tableWidget->item(i,j)!=0){
                    rowStr+=ui->tableWidget->item(i,j)->text()+",";
                }
                else{
                    QMessageBox::information(0,tr("提示"),tr("增加的当前行数据为空"));
                    break;
                }
            }
            list.push_back(rowStr);
        }                                         //将表格中的数据存入list
        w.entOut(list,filepath);                  //函数调用
    }else{
        QMessageBox::information(this,QString("提示"),QString("文件名不存在"),QMessageBox::Ok);
    }
    ui->tableWidget->clearContents();
}

void outlDialog::on_pushButton_3_clicked()
{
    OutDialog *w=new OutDialog;
    w->show();
    this->hide();
}
